package com.base.moduleUser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.base.core.common.http.response.AjaxResult;
import com.base.core.common.utils.UserContext;
import com.base.core.domain.User;
import com.base.core.exception.BusinessException;
import com.base.moduleUser.dto.RegisterDto;
import com.base.moduleUser.dto.UpdateLocationDto;
import com.base.moduleUser.service.UserService;
import com.base.moduleUser.vo.UserLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/virtual/user")
public class UserController {

    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user) throws BusinessException {
        /**
         * 测试用户数据
         */
        UserLoginVo userLoginVO=userService.login(user);

        if(ObjectUtils.isEmpty(userLoginVO)){
            return AjaxResult.error("用户名或密码错误");
        }
        return AjaxResult.success(userLoginVO);
    }

    /**
     * 注册用户
     * @param registerDto
     * @return
     */

     @PostMapping("/register")
     public AjaxResult register(@RequestBody RegisterDto registerDto){
         log.info("注册接口，注册信息：{}", registerDto);
         System.out.println("注册信息：" + registerDto);
         return userService.register(registerDto);
     }

    /**
     * 删除用户
     * @param
     */
     @PostMapping("/delete")
     public String delete() {
         User user2 = new User();
         user2.setUsername("admin2");
         user2.setPassword("123456");
         user2.setAvatarUrl("http://www.baidu.com");
         userService.delete(user2);
         return "delete success";
     }

     //缓存用户信息
     @PostMapping("/cash")
     public String cash(User user) {
         User user3 = new User();
         user3.setUsername("admin3");
         user3.setPassword("123456");
         user3.setAvatarUrl("http://www.baidu.com");
         userService.cash(user3);
         return "cash success";
     }

    /**
     * 验证用户名
     */
    @GetMapping("/checkUsernameExists")
    public AjaxResult checkUsernameExists(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userService.getOne(queryWrapper);
        if(ObjectUtils.isEmpty(user)){
            return AjaxResult.success("",false);
        }else{
            return AjaxResult.success("",true);
        }
    }

    /**
     * 发送验证码
     */
    @GetMapping("/getCaptcha")
    public AjaxResult getCaptcha(@RequestParam(value = "phone") String phone) {
        log.info("获取验证码接口，手机号：{}", phone);
        return userService.getCaptcha(phone);
    }

    /**
     * 验证验证码
     */
    @PostMapping("/verifyCaptcha")
    public AjaxResult verifyCaptcha(@RequestParam String phone, @RequestParam String captcha) {
        log.info("验证码验证接口，手机号：{}, 验证码：{}", phone, captcha);
        return userService.verifyCaptcha(phone, captcha);
    }

    /**
     * 更新用户地理位置信息
     */
    @PostMapping("/updateLocation")
    public AjaxResult updateLocation(@RequestBody UpdateLocationDto updateLocationDto){
        log.info("<UNK>{}", updateLocationDto);
        userService.updateLocation(updateLocationDto);
        return AjaxResult.success("更新成功");
    }



}
